(function () {
    "use strict";

    angular.module('core').run(run);
    run.$inject = ['$rootScope', '$timeout', '$state', '$confirmModalDefaults'];

    function run($rootScope, $timeout, $state, $confirmModalDefaults) {

        $rootScope.$on("$stateChangeStart", function (event, toState, toStateParams, fromState, fromStateParams) {
            var scope = angular.element($('[ng-controller="_layoutController"]')).scope();
            scope.carregando = true;
        });

        $rootScope.$on("$stateChangeSuccess", function (event, toState, toStateParams, fromState, fromStateParams) {
            var scope = angular.element($('[ng-controller="_layoutController"]')).scope();
            scope.carregando = false;
        });

        $rootScope.$on("$stateChangeError", function (event, toState, toParams, fromState, fromParams, error) {
            var scope = angular.element($('[ng-controller="_layoutController"]')).scope();
            scope.carregando = false;

            if (error.status == 401)
                $state.go("state-login");
        });

        /* default para o módulo angular-confirm */
        $confirmModalDefaults.defaultLabels.title = 'Confirmação';
        $confirmModalDefaults.defaultLabels.ok = 'OK, Confirmado';
        $confirmModalDefaults.defaultLabels.cancel = 'Voltar';

        activateOnScroll();

        ///TODO: transformar em angular futuramente.
        $(window).scroll(function () {
            activateOnScroll();
        });

        function activateOnScroll() {
            if ($('.activate-on-scroll').length) {

                if ($(window).scrollTop() > 1) {
                    $('header').removeClass('navbar-transparent');
                }
                else {
                    $('header').addClass('navbar-transparent');
                }
            }
        }
    }

})();